-- -------------------------------------------------------------------------
-- Utils/VehicleCustomizer.lua
-- -------------------------------------------------------------------------

local module = {}

-- Dependencies
local debugger = nil

-- [Natives Cache]
local N = {}

-- -------------------------------------------------------------------------
-- Mod Slot Constants
-- -------------------------------------------------------------------------

module.MOD_SLOTS = {
    SPOILER = 0,
    FRONT_BUMPER = 1,
    REAR_BUMPER = 2,
    SIDESKIRT = 3,
    EXHAUST = 4,
    CHASSIS = 5,
    GRILLE = 6,
    HOOD = 7,
    FENDER = 8,
    RIGHT_FENDER = 9,
    ROOF = 10,
    ENGINE = 11,
    BRAKES = 12,
    TRANSMISSION = 13,
    HORNS = 14,
    SUSPENSION = 15,
    ARMOR = 16,
    NITROUS = 17,
    TURBO = 18,
    SUBWOOFER = 19,
    TIRE_SMOKE = 20,
    HYDRAULICS = 21,
    XENON = 22,
    FRONT_WHEELS = 23,
    BACK_WHEELS = 24,
    TRIM = 27,
    ORNAMENTS = 28,
    DASHBOARD = 29,
    DIAL = 30,
    DOOR_SPEAKER = 31,
    SEATS = 32,
    STEERING_WHEEL = 33,
    SHIFTER = 34,
    PLAQUES = 35,
    SPEAKERS = 36,
    TRUNK = 37,
    HYDRAULICS_2 = 38,
    ENGINE_BLOCK = 39,
    AIR_FILTER = 40,
    STRUTS = 41,
    ARCH_COVER = 42,
    AERIALS = 43,
    TRIM_2 = 44,
    TANK = 45,
    LEFT_DOOR = 46,
    RIGHT_DOOR = 47,
    LIVERY = 48,
    PLATE_HOLDER = 49,
    VANITY_PLATES = 50,
    LIGHTBAR = 53,
}

-- Plate types
module.PLATE_TYPES = {
    "Blue on White",
    "Yellow on Black",
    "Yellow on Blue",
    "Blue on White 2",
    "Blue on White 3",
    "Yankton",
    "White on Red",
    "Las Venturas",
    "Liberty City",
    "Blue on White/Red",
    "Yellow on Purple",
    "Yellow on Black 2",
    "White on Green",
}

-- Window tint levels
module.WINDOW_TINTS = {
    "None",
    "Pure Black",
    "Dark Smoke",
    "Light Smoke",
    "Stock",
    "Limo",
    "Green",
}

-- Neon positions
module.NEON = {
    LEFT = 0,
    RIGHT = 1,
    FRONT = 2,
    BACK = 3,
}

-- -------------------------------------------------------------------------
-- Helper Functions
-- -------------------------------------------------------------------------

local pcall <const> = pcall
local ipairs <const> = ipairs

local function ensure_control(vehicle)
    if request and request.control and vehicle and vehicle ~= 0 then
        pcall(request.control, vehicle, true)
    end
end

local function does_entity_exist(entity)
    if not entity or entity == 0 then return false end
    if not N.DOES_ENTITY_EXIST then return true end  -- Assume it exists when the native is not cached.
    local ok, result = pcall(N.DOES_ENTITY_EXIST, entity)
    return ok and result  -- Accept any truthy value (true, 1, etc.).
end

local function is_motorcycle(vehicle)
    if not N.GET_VEHICLE_CLASS then return false end
    local ok, result = pcall(N.GET_VEHICLE_CLASS, vehicle)
    return ok and result == 8
end

-- -------------------------------------------------------------------------
-- Performance Mods
-- -------------------------------------------------------------------------

function module.apply_max_performance(vehicle)
    if not does_entity_exist(vehicle) then return false end

    ensure_control(vehicle)

    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end

    if N.GET_NUM_VEHICLE_MODS and N.SET_VEHICLE_MOD then
        local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, 11)
        if ok and count and count > 0 then
            pcall(N.SET_VEHICLE_MOD, vehicle, 11, count - 1, false)
        end
    end

    if N.GET_NUM_VEHICLE_MODS and N.SET_VEHICLE_MOD then
        local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, 12)
        if ok and count and count > 0 then
            pcall(N.SET_VEHICLE_MOD, vehicle, 12, count - 1, false)
        end
    end

    if N.GET_NUM_VEHICLE_MODS and N.SET_VEHICLE_MOD then
        local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, 13)
        if ok and count and count > 0 then
            pcall(N.SET_VEHICLE_MOD, vehicle, 13, count - 1, false)
        end
    end

    if N.GET_NUM_VEHICLE_MODS and N.SET_VEHICLE_MOD then
        local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, 15)
        if ok and count and count > 0 then
            pcall(N.SET_VEHICLE_MOD, vehicle, 15, count - 1, false)
        end
    end

    if N.GET_NUM_VEHICLE_MODS and N.SET_VEHICLE_MOD then
        local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, 16)
        if ok and count and count > 0 then
            pcall(N.SET_VEHICLE_MOD, vehicle, 16, count - 1, false)
        end
    end

    if N.TOGGLE_VEHICLE_MOD then
        pcall(N.TOGGLE_VEHICLE_MOD, vehicle, 18, true)
    end

    if N.SET_VEHICLE_TYRES_CAN_BURST then
        pcall(N.SET_VEHICLE_TYRES_CAN_BURST, vehicle, false)
    end

    return true
end

-- -------------------------------------------------------------------------
-- Color Functions
-- -------------------------------------------------------------------------

function module.set_vehicle_color(vehicle, primary, secondary)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_COLOURS then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_COLOURS, vehicle, primary or 0, secondary or 0)
    return true
end

function module.set_custom_primary_color(vehicle, r, g, b)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR, vehicle, r, g, b)
    return true
end

function module.set_custom_secondary_color(vehicle, r, g, b)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR, vehicle, r, g, b)
    return true
end

function module.set_pearlescent_color(vehicle, color_id)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_EXTRA_COLOURS then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_EXTRA_COLOURS, vehicle, color_id, 0)
    return true
end

function module.set_wheel_color(vehicle, color_id)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_EXTRA_COLOURS then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_EXTRA_COLOURS, vehicle, 0, color_id)
    return true
end

-- -------------------------------------------------------------------------
-- Neon Functions
-- -------------------------------------------------------------------------

function module.set_neon_enabled(vehicle, position, enabled)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_NEON_ENABLED then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_NEON_ENABLED, vehicle, position, enabled)
    return true
end

function module.set_all_neons_enabled(vehicle, enabled)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_NEON_ENABLED then return false end
    for i = 0, 3 do
        N.SET_VEHICLE_NEON_ENABLED(vehicle, i, enabled)
    end
    return true
end

function module.set_neon_color(vehicle, r, g, b)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_NEON_COLOUR then return false end
    N.SET_VEHICLE_NEON_COLOUR(vehicle, r, g, b)
    return true
end

-- -------------------------------------------------------------------------
-- Xenon Headlights
-- -------------------------------------------------------------------------

function module.set_xenon_enabled(vehicle, enabled)
    if not does_entity_exist(vehicle) then return false end
    if not N.TOGGLE_VEHICLE_MOD then return false end
    N.TOGGLE_VEHICLE_MOD(vehicle, 22, enabled)
    return true
end

function module.set_xenon_color(vehicle, color_index)
    if not does_entity_exist(vehicle) then return false end
    if N.TOGGLE_VEHICLE_MOD then
        N.TOGGLE_VEHICLE_MOD(vehicle, 22, true)
    end
    if N.SET_VEHICLE_XENON_LIGHT_COLOR_INDEX then
        N.SET_VEHICLE_XENON_LIGHT_COLOR_INDEX(vehicle, color_index)
    end
    return true
end

-- -------------------------------------------------------------------------
-- Wheels
-- -------------------------------------------------------------------------

function module.set_wheel_type(vehicle, wheel_type)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_WHEEL_TYPE then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_WHEEL_TYPE, vehicle, wheel_type)
    return true
end

function module.set_front_wheels(vehicle, wheel_index, custom_tires)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end
    if N.SET_VEHICLE_MOD then
        pcall(N.SET_VEHICLE_MOD, vehicle, 23, wheel_index, custom_tires or false)
    end
    return true
end

function module.set_back_wheels(vehicle, wheel_index, custom_tires)
    if not does_entity_exist(vehicle) then return false end
    if not is_motorcycle(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end
    if N.SET_VEHICLE_MOD then
        pcall(N.SET_VEHICLE_MOD, vehicle, 24, wheel_index, custom_tires or false)
    end
    return true
end

-- -------------------------------------------------------------------------
-- Tire Smoke
-- -------------------------------------------------------------------------

function module.set_tire_smoke_enabled(vehicle, enabled)
    if not does_entity_exist(vehicle) then return false end
    if not N.TOGGLE_VEHICLE_MOD then return false end
    N.TOGGLE_VEHICLE_MOD(vehicle, 20, enabled)
    return true
end

function module.set_tire_smoke_color(vehicle, r, g, b)
    if not does_entity_exist(vehicle) then return false end
    if N.TOGGLE_VEHICLE_MOD then
        N.TOGGLE_VEHICLE_MOD(vehicle, 20, true)
    end
    if N.SET_VEHICLE_TYRE_SMOKE_COLOR then
        N.SET_VEHICLE_TYRE_SMOKE_COLOR(vehicle, r, g, b)
    end
    return true
end

-- -------------------------------------------------------------------------
-- Plate
-- -------------------------------------------------------------------------

function module.set_plate_text(vehicle, text)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_NUMBER_PLATE_TEXT then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT, vehicle, text)
    return true
end

function module.set_plate_type(vehicle, plate_type)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX, vehicle, plate_type)
    return true
end

-- -------------------------------------------------------------------------
-- Window Tint
-- -------------------------------------------------------------------------

function module.set_window_tint(vehicle, tint_level)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_WINDOW_TINT then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_WINDOW_TINT, vehicle, tint_level)
    return true
end

-- -------------------------------------------------------------------------
-- Interior
-- -------------------------------------------------------------------------

function module.set_interior_color(vehicle, color_id)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_INTERIOR_COLOUR then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_INTERIOR_COLOUR, vehicle, color_id)
    return true
end

function module.set_dashboard_color(vehicle, color_id)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_DASHBOARD_COLOUR then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_DASHBOARD_COLOUR, vehicle, color_id)
    return true
end

-- -------------------------------------------------------------------------
-- Livery
-- -------------------------------------------------------------------------

function module.set_livery(vehicle, livery_index)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end
    if N.SET_VEHICLE_MOD then
        pcall(N.SET_VEHICLE_MOD, vehicle, 48, livery_index, false)
    end
    if N.SET_VEHICLE_LIVERY then
        pcall(N.SET_VEHICLE_LIVERY, vehicle, livery_index)
    end
    return true
end

-- -------------------------------------------------------------------------
-- Extras
-- -------------------------------------------------------------------------

function module.set_extra(vehicle, extra_id, enabled)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)

    if N.DOES_EXTRA_EXIST then
        local ok, exists = pcall(N.DOES_EXTRA_EXIST, vehicle, extra_id)
        if ok and exists and N.SET_VEHICLE_EXTRA then
            -- SET_VEHICLE_EXTRA uses inverted logic: false = on, true = off
            pcall(N.SET_VEHICLE_EXTRA, vehicle, extra_id, not enabled)
            return true
        end
    end
    return false
end

-- -------------------------------------------------------------------------
-- Generic Mod Functions
-- -------------------------------------------------------------------------

function module.set_mod(vehicle, mod_slot, mod_index, custom_tires)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end
    if N.SET_VEHICLE_MOD then
        pcall(N.SET_VEHICLE_MOD, vehicle, mod_slot, mod_index, custom_tires or false)
    end
    return true
end

function module.get_num_mods(vehicle, mod_slot)
    if not does_entity_exist(vehicle) then return 0 end
    if not N.GET_NUM_VEHICLE_MODS then return 0 end

    -- Ensure the mod kit is initialized (required by some vehicles).
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end

    local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, mod_slot)
    if ok and count then
        return count
    end
    return 0
end

function module.toggle_mod(vehicle, mod_slot, enabled)
    if not does_entity_exist(vehicle) then return false end
    if not N.TOGGLE_VEHICLE_MOD then return false end
    ensure_control(vehicle)
    pcall(N.TOGGLE_VEHICLE_MOD, vehicle, mod_slot, enabled)
    return true
end

function module.clean_vehicle(vehicle)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_DIRT_LEVEL then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_DIRT_LEVEL, vehicle, 0.0)
    return true
end

function module.set_bulletproof_tires(vehicle, enabled)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_VEHICLE_TYRES_CAN_BURST then return false end
    ensure_control(vehicle)
    pcall(N.SET_VEHICLE_TYRES_CAN_BURST, vehicle, not enabled)
    return true
end

function module.set_invincible(vehicle, enabled)
    if not does_entity_exist(vehicle) then return false end
    if not N.SET_ENTITY_INVINCIBLE then return false end
    ensure_control(vehicle)
    pcall(N.SET_ENTITY_INVINCIBLE, vehicle, enabled)
    return true
end

function module.apply_max_visual_upgrades(vehicle)
    if not does_entity_exist(vehicle) then return false end

    ensure_control(vehicle)

    -- Enable mod kit
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end

    -- Visual mod slots (excludes performance: 11-13, 15-18)
    local visual_slots = {
        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
        14, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
    }

    if N.GET_NUM_VEHICLE_MODS and N.SET_VEHICLE_MOD then
        for _, slot in ipairs(visual_slots) do
            local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, slot)
            if ok and count and count > 0 then
                pcall(N.SET_VEHICLE_MOD, vehicle, slot, count - 1, false)
            end
        end
    end

    -- Livery
    if N.GET_NUM_VEHICLE_MODS and N.SET_VEHICLE_MOD and N.SET_VEHICLE_LIVERY then
        local ok, count = pcall(N.GET_NUM_VEHICLE_MODS, vehicle, 48)
        if ok and count and count > 0 then
            pcall(N.SET_VEHICLE_MOD, vehicle, 48, count - 1, false)
            pcall(N.SET_VEHICLE_LIVERY, vehicle, count - 1)
        end
    end

    return true
end

-- -------------------------------------------------------------------------
-- Utility Functions
-- -------------------------------------------------------------------------

function module.hex_to_rgb(hex)
    hex = hex:gsub("#", "")
    if #hex ~= 6 then return 0, 0, 0 end

    local r = tonumber("0x" .. hex:sub(1, 2))
    local g = tonumber("0x" .. hex:sub(3, 4))
    local b = tonumber("0x" .. hex:sub(5, 6))

    return r or 0, g or 0, b or 0
end

function module.rgb_to_hex(r, g, b)
    return string.format("%02X%02X%02X", r, g, b)
end

-- -------------------------------------------------------------------------
-- Preset Data (for UI dropdowns)
-- -------------------------------------------------------------------------

module.PAINT_PRESETS = {
    { name = "None",         value = nil },
    { name = "Black",        value = { r = 0, g = 0, b = 0 } },
    { name = "White",        value = { r = 255, g = 255, b = 255 } },
    { name = "Red",          value = { r = 255, g = 0, b = 0 } },
    { name = "Green",        value = { r = 0, g = 255, b = 0 } },
    { name = "Blue",         value = { r = 0, g = 0, b = 255 } },
    { name = "Yellow",       value = { r = 255, g = 255, b = 0 } },
    { name = "Orange",       value = { r = 255, g = 165, b = 0 } },
    { name = "Purple",       value = { r = 128, g = 0, b = 128 } },
    { name = "Pink",         value = { r = 255, g = 105, b = 180 } },
    { name = "Cyan",         value = { r = 0, g = 255, b = 255 } },
    { name = "Gold",         value = { r = 255, g = 215, b = 0 } },
    { name = "Silver",       value = { r = 192, g = 192, b = 192 } },
    { name = "Matte Black",  value = { r = 20, g = 20, b = 20 } },
    { name = "Chrome",       value = { r = 220, g = 220, b = 220 } },
}

module.CHAMELEON_PRESETS = {
    { name = "None",                    id = -1 },
    { name = "Anodized Red",        id = 161 },
    { name = "Anodized Wine",     id = 162 },
    { name = "Anodized Purple",     id = 163 },
    { name = "Anodized Blue",       id = 164 },
    { name = "Anodized Green",      id = 165 },
    { name = "Anodized Lime",     id = 166 },
    { name = "Anodized Copper",     id = 167 },
    { name = "Anodized Bronze",   id = 168 },
    { name = "Anodized Champagne", id = 169 },
    { name = "Anodized Gold",       id = 170 },
    { name = "Green/Blue Flip",  id = 171 },
    { name = "Green/Red Flip",   id = 172 },
    { name = "Green/Brown Flip", id = 173 },
    { name = "Green/Turquoise Flip", id = 174 },
    { name = "Green/Purple Flip", id = 175 },
    { name = "Teal/Purple Flip", id = 176 },
    { name = "Turquoise/Red Flip", id = 177 },
    { name = "Turquoise/Purple Flip", id = 178 },
    { name = "Cyan/Purple Flip", id = 179 },
    { name = "Blue/Pink Flip",   id = 180 },
    { name = "Blue/Green Flip",  id = 181 },
    { name = "Purple/Red Flip",  id = 182 },
    { name = "Purple/Green Flip", id = 183 },
    { name = "Magenta/Green Flip", id = 184 },
    { name = "Magenta/Yellow Flip", id = 185 },
    { name = "Burgundy/Green Flip", id = 186 },
    { name = "Magenta/Cyan Flip", id = 187 },
    { name = "Copper/Purple Flip", id = 188 },
    { name = "Magenta/Orange Flip", id = 189 },
    { name = "Red/Orange Flip",  id = 190 },
    { name = "Orange/Purple Flip", id = 191 },
    { name = "Orange/Blue Flip", id = 192 },
    { name = "White/Purple Flip", id = 193 },
    { name = "Red/Rainbow Flip", id = 194 },
    { name = "Blue/Rainbow Flip", id = 195 },
    { name = "Dark Green Pearl",  id = 196 },
    { name = "Dark Teal Pearl",   id = 197 },
    { name = "Dark Blue Pearl",   id = 198 },
    { name = "Dark Purple Pearl", id = 199 },
    { name = "Oil Slick Pearl",   id = 200 },
    { name = "Light Green Pearl", id = 201 },
    { name = "Light Blue Pearl",  id = 202 },
    { name = "Light Purple Pearl", id = 203 },
    { name = "Light Pink Pearl",  id = 204 },
    { name = "Off White Prismatic", id = 205 },
    { name = "Pink Pearl",        id = 206 },
    { name = "Yellow Pearl",      id = 207 },
    { name = "Green Pearl",       id = 208 },
    { name = "Blue Pearl",        id = 209 },
    { name = "Cream Pearl",       id = 210 },
    { name = "White Prismatic",   id = 211 },
    { name = "Graphite Prismatic", id = 212 },
    { name = "Dark Blue Prismatic", id = 213 },
    { name = "Dark Purple Prismatic", id = 214 },
    { name = "Hot Pink Prismatic", id = 215 },
    { name = "Red Prismatic",     id = 216 },
    { name = "Green Prismatic",   id = 217 },
    { name = "Black Prismatic",   id = 218 },
    { name = "Oil Slick Prismatic", id = 219 },
    { name = "Rainbow Prismatic", id = 220 },
    { name = "Black Holographic", id = 221 },
    { name = "White Holographic", id = 222 },
}

module.NEON_PRESETS = {
    { name = "None",         value = nil },
    { name = "White",        value = { r = 255, g = 255, b = 255 } },
    { name = "Blue",         value = { r = 0, g = 0, b = 255 } },
    { name = "Electric Blue", value = { r = 0, g = 150, b = 255 } },
    { name = "Mint Green",   value = { r = 0, g = 255, b = 140 } },
    { name = "Lime Green",   value = { r = 0, g = 255, b = 0 } },
    { name = "Yellow",       value = { r = 255, g = 255, b = 0 } },
    { name = "Golden",       value = { r = 255, g = 200, b = 0 } },
    { name = "Orange",       value = { r = 255, g = 100, b = 0 } },
    { name = "Red",          value = { r = 255, g = 0, b = 0 } },
    { name = "Pink",         value = { r = 255, g = 0, b = 255 } },
    { name = "Hot Pink",     value = { r = 255, g = 50, b = 150 } },
    { name = "Purple",       value = { r = 150, g = 0, b = 255 } },
}

module.SMOKE_PRESETS = {
    { name = "None",         value = nil },
    { name = "White",        value = { r = 255, g = 255, b = 255 } },
    { name = "Black",        value = { r = 20, g = 20, b = 20 } },
    { name = "Blue",         value = { r = 0, g = 150, b = 255 } },
    { name = "Yellow",       value = { r = 255, g = 255, b = 0 } },
    { name = "Orange",       value = { r = 255, g = 128, b = 0 } },
    { name = "Red",          value = { r = 255, g = 0, b = 0 } },
    { name = "Green",        value = { r = 0, g = 255, b = 0 } },
    { name = "Purple",       value = { r = 150, g = 0, b = 255 } },
    { name = "Pink",         value = { r = 255, g = 50, b = 150 } },
    { name = "Patriot",      value = { r = 0, g = 0, b = 255 } },
}

module.XENON_PRESETS = {
    { name = "Default",       id = -1 },
    { name = "White",         id = 0 },
    { name = "Blue",          id = 1 },
    { name = "Electric Blue", id = 2 },
    { name = "Mint Green",    id = 3 },
    { name = "Lime Green",    id = 4 },
    { name = "Yellow",        id = 5 },
    { name = "Golden",        id = 6 },
    { name = "Orange",        id = 7 },
    { name = "Red",           id = 8 },
    { name = "Pony Pink",     id = 9 },
    { name = "Hot Pink",      id = 10 },
    { name = "Purple",        id = 11 },
    { name = "Blacklight",    id = 12 },
}

module.WHEEL_TYPES = {
    { name = "Stock",         id = -1 },
    { name = "Sport",         id = 0 },
    { name = "Muscle",        id = 1 },
    { name = "Lowrider",      id = 2 },
    { name = "SUV",           id = 3 },
    { name = "Offroad",       id = 4 },
    { name = "Tuner",         id = 5 },
    { name = "Biker",         id = 6 },
    { name = "High End",      id = 7 },
    { name = "Benny's Original", id = 8 },
    { name = "Benny's Bespoke", id = 9 },
    { name = "Open Wheel",    id = 10 },
    { name = "Street",        id = 11 },
    { name = "Track",         id = 12 },
}

module.THEME_PACKS = {
    { name = "None", value = nil },
    {
        name = "Ice",
        value = {
            paint = { r = 200, g = 230, b = 255 },
            neon = { r = 0, g = 200, b = 255 },
            smoke = { r = 200, g = 230, b = 255 },
            xenon = 2,
        }
    },
    {
        name = "Fire",
        value = {
            paint = { r = 255, g = 50, b = 0 },
            neon = { r = 255, g = 100, b = 0 },
            smoke = { r = 255, g = 50, b = 0 },
            xenon = 7,
        }
    },
    {
        name = "Toxic",
        value = {
            paint = { r = 0, g = 255, b = 0 },
            neon = { r = 0, g = 255, b = 100 },
            smoke = { r = 0, g = 255, b = 0 },
            xenon = 4,
        }
    },
    {
        name = "Royal",
        value = {
            paint = { r = 100, g = 0, b = 200 },
            neon = { r = 150, g = 0, b = 255 },
            smoke = { r = 100, g = 0, b = 200 },
            xenon = 11,
        }
    },
    {
        name = "Sunset",
        value = {
            paint = { r = 255, g = 100, b = 50 },
            neon = { r = 255, g = 150, b = 0 },
            smoke = { r = 255, g = 100, b = 0 },
            xenon = 6,
        }
    },
    {
        name = "Midnight",
        value = {
            paint = { r = 20, g = 20, b = 40 },
            neon = { r = 100, g = 0, b = 200 },
            smoke = { r = 50, g = 0, b = 100 },
            xenon = 12,
        }
    },
    {
        name = "Gold Rush",
        value = {
            paint = { r = 255, g = 200, b = 0 },
            neon = { r = 255, g = 180, b = 0 },
            smoke = { r = 255, g = 200, b = 50 },
            xenon = 5,
        }
    },
    {
        name = "Cotton Candy",
        value = {
            paint = { r = 255, g = 150, b = 200 },
            neon = { r = 255, g = 100, b = 200 },
            smoke = { r = 200, g = 100, b = 255 },
            xenon = 10,
        }
    },
    {
        name = "Stealth",
        value = {
            paint = { r = 20, g = 20, b = 20 },
            neon = nil,
            smoke = { r = 20, g = 20, b = 20 },
            xenon = -1,
        }
    },
}

-- -------------------------------------------------------------------------
-- High-Level Application Functions (for UI integration)
-- -------------------------------------------------------------------------

function module.init(natives_lib, debug_lib)
    debugger = debug_lib

    -- Cache Natives 
    if natives_lib then
        local function get(name) return natives_lib[name] end

        -- Vehicle Mods
        N.SET_VEHICLE_MOD_KIT = get("SET_VEHICLE_MOD_KIT")
        N.GET_NUM_VEHICLE_MODS = get("GET_NUM_VEHICLE_MODS")
        N.GET_VEHICLE_MOD = get("GET_VEHICLE_MOD")
        N.SET_VEHICLE_MOD = get("SET_VEHICLE_MOD")
        N.TOGGLE_VEHICLE_MOD = get("TOGGLE_VEHICLE_MOD")

        -- Vehicle Repair & Clean
        N.SET_VEHICLE_FIXED = get("SET_VEHICLE_FIXED")
        N.SET_VEHICLE_DIRT_LEVEL = get("SET_VEHICLE_DIRT_LEVEL")
        N.SET_VEHICLE_DEFORMATION_FIXED = get("SET_VEHICLE_DEFORMATION_FIXED")

        -- Vehicle Colors
        N.SET_VEHICLE_COLOURS = get("SET_VEHICLE_COLOURS")
        N.GET_VEHICLE_COLOURS = get("GET_VEHICLE_COLOURS")
        N.SET_VEHICLE_EXTRA_COLOURS = get("SET_VEHICLE_EXTRA_COLOURS")
        N.GET_VEHICLE_EXTRA_COLOURS = get("GET_VEHICLE_EXTRA_COLOURS")
        N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR = get("SET_VEHICLE_CUSTOM_PRIMARY_COLOUR")
        N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR = get("SET_VEHICLE_CUSTOM_SECONDARY_COLOUR")
        N.GET_VEHICLE_CUSTOM_PRIMARY_COLOUR = get("GET_VEHICLE_CUSTOM_PRIMARY_COLOUR")
        N.GET_VEHICLE_CUSTOM_SECONDARY_COLOUR = get("GET_VEHICLE_CUSTOM_SECONDARY_COLOUR")

        -- Neon
        N.SET_VEHICLE_NEON_ENABLED = get("SET_VEHICLE_NEON_ENABLED")
        N.GET_VEHICLE_NEON_ENABLED = get("GET_VEHICLE_NEON_ENABLED")
        N.SET_VEHICLE_NEON_COLOUR = get("SET_VEHICLE_NEON_COLOUR")
        N.GET_VEHICLE_NEON_COLOUR = get("GET_VEHICLE_NEON_COLOUR")

        -- Xenon
        N.SET_VEHICLE_XENON_LIGHT_COLOR_INDEX = get("SET_VEHICLE_XENON_LIGHT_COLOR_INDEX")
        N.GET_VEHICLE_XENON_LIGHTS_CUSTOM_COLOR = get("GET_VEHICLE_XENON_LIGHTS_CUSTOM_COLOR")

        -- Wheels
        N.SET_VEHICLE_WHEEL_TYPE = get("SET_VEHICLE_WHEEL_TYPE")
        N.GET_VEHICLE_WHEEL_TYPE = get("GET_VEHICLE_WHEEL_TYPE")

        -- Tire Smoke
        N.SET_VEHICLE_TYRE_SMOKE_COLOR = get("SET_VEHICLE_TYRE_SMOKE_COLOR")
        N.GET_VEHICLE_TYRE_SMOKE_COLOR = get("GET_VEHICLE_TYRE_SMOKE_COLOR")

        -- Plate
        N.SET_VEHICLE_NUMBER_PLATE_TEXT = get("SET_VEHICLE_NUMBER_PLATE_TEXT")
        N.GET_VEHICLE_NUMBER_PLATE_TEXT = get("GET_VEHICLE_NUMBER_PLATE_TEXT")
        N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX = get("SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX")
        N.GET_VEHICLE_NUMBER_PLATE_TEXT_INDEX = get("GET_VEHICLE_NUMBER_PLATE_TEXT_INDEX")

        -- Window Tint
        N.SET_VEHICLE_WINDOW_TINT = get("SET_VEHICLE_WINDOW_TINT")
        N.GET_VEHICLE_WINDOW_TINT = get("GET_VEHICLE_WINDOW_TINT")

        -- Interior
        N.SET_VEHICLE_INTERIOR_COLOUR = get("SET_VEHICLE_INTERIOR_COLOUR")
        N.GET_VEHICLE_INTERIOR_COLOUR = get("GET_VEHICLE_INTERIOR_COLOUR")
        N.SET_VEHICLE_DASHBOARD_COLOUR = get("SET_VEHICLE_DASHBOARD_COLOUR")
        N.GET_VEHICLE_DASHBOARD_COLOUR = get("GET_VEHICLE_DASHBOARD_COLOUR")

        -- Livery
        N.SET_VEHICLE_LIVERY = get("SET_VEHICLE_LIVERY")
        N.GET_VEHICLE_LIVERY = get("GET_VEHICLE_LIVERY")

        -- Extras
        N.DOES_EXTRA_EXIST = get("DOES_EXTRA_EXIST")
        N.SET_VEHICLE_EXTRA = get("SET_VEHICLE_EXTRA")
        N.IS_VEHICLE_EXTRA_TURNED_ON = get("IS_VEHICLE_EXTRA_TURNED_ON")

        -- Entity
        N.DOES_ENTITY_EXIST = get("DOES_ENTITY_EXIST")
        N.GET_ENTITY_MODEL = get("GET_ENTITY_MODEL")
        N.SET_ENTITY_INVINCIBLE = get("SET_ENTITY_INVINCIBLE")
        N.SET_VEHICLE_TYRES_CAN_BURST = get("SET_VEHICLE_TYRES_CAN_BURST")
        N.GET_VEHICLE_TYRES_CAN_BURST = get("GET_VEHICLE_TYRES_CAN_BURST")

        -- Vehicle Class
        N.GET_VEHICLE_CLASS = get("GET_VEHICLE_CLASS")

        -- Door Control
        N.SET_VEHICLE_DOOR_OPEN = get("SET_VEHICLE_DOOR_OPEN")
        N.SET_VEHICLE_DOOR_SHUT = get("SET_VEHICLE_DOOR_SHUT")

        -- Window Control
        N.ROLL_DOWN_WINDOW = get("ROLL_DOWN_WINDOW")
        N.ROLL_UP_WINDOW = get("ROLL_UP_WINDOW")
        N.SMASH_VEHICLE_WINDOW = get("SMASH_VEHICLE_WINDOW")
        N.FIX_VEHICLE_WINDOW = get("FIX_VEHICLE_WINDOW")

        -- Convertible Roof
        N.IS_VEHICLE_A_CONVERTIBLE = get("IS_VEHICLE_A_CONVERTIBLE")
        N.RAISE_CONVERTIBLE_ROOF = get("RAISE_CONVERTIBLE_ROOF")
        N.LOWER_CONVERTIBLE_ROOF = get("LOWER_CONVERTIBLE_ROOF")
        N.GET_CONVERTIBLE_ROOF_STATE = get("GET_CONVERTIBLE_ROOF_STATE")

        -- Engine/Lights
        N.SET_VEHICLE_ENGINE_ON = get("SET_VEHICLE_ENGINE_ON")
        N.SET_VEHICLE_LIGHTS = get("SET_VEHICLE_LIGHTS")
        N.SET_VEHICLE_INDICATOR_LIGHTS = get("SET_VEHICLE_INDICATOR_LIGHTS")

        if debugger and debugger.debug then
            debugger.debug("[VehicleCustomizer] Natives cached successfully")
        end
    else
        print("[VehicleCustomizer] WARNING: natives_lib not provided")
    end

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Module initialized")
    end

    return true
end

function module.build_dropdown_options(preset_table, translate)
    local options = {}
    local translate_fn = type(translate) == "function" and translate or nil
    for i, preset in ipairs(preset_table) do
        local name = preset.name
        options[i] = translate_fn and translate_fn(name) or name
    end
    return options
end

function module.apply_theme_pack(vehicle, theme)
    if not does_entity_exist(vehicle) then return false end
    if not theme or not theme.value then return false end

    local v = theme.value

    -- Apply paint
    if v.paint then
        module.set_custom_primary_color(vehicle, v.paint.r, v.paint.g, v.paint.b)
        module.set_custom_secondary_color(vehicle, v.paint.r, v.paint.g, v.paint.b)
    end

    -- Apply neon
    if v.neon then
        module.set_all_neons_enabled(vehicle, true)
        module.set_neon_color(vehicle, v.neon.r, v.neon.g, v.neon.b)
    else
        module.set_all_neons_enabled(vehicle, false)
    end

    -- Apply tire smoke
    if v.smoke then
        module.set_tire_smoke_color(vehicle, v.smoke.r, v.smoke.g, v.smoke.b)
    end

    -- Apply xenon
    if v.xenon then
        module.set_xenon_color(vehicle, v.xenon)
    end

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied theme pack", { name = theme.name })
    end

    return true
end

function module.apply_paint(vehicle, preset)
    if not does_entity_exist(vehicle) then return false end
    if not preset or not preset.value then return false end

    local c = preset.value
    module.set_custom_primary_color(vehicle, c.r, c.g, c.b)
    module.set_custom_secondary_color(vehicle, c.r, c.g, c.b)

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied paint", { name = preset.name })
    end

    return true
end

function module.apply_neon(vehicle, preset)
    if not does_entity_exist(vehicle) then return false end

    if not preset or not preset.value then
        module.set_all_neons_enabled(vehicle, false)
        return true
    end

    local c = preset.value
    module.set_all_neons_enabled(vehicle, true)
    module.set_neon_color(vehicle, c.r, c.g, c.b)

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied neon", { name = preset.name })
    end

    return true
end

function module.apply_tire_smoke(vehicle, preset)
    if not does_entity_exist(vehicle) then return false end

    if not preset or not preset.value then
        module.set_tire_smoke_enabled(vehicle, false)
        return true
    end

    local c = preset.value
    module.set_tire_smoke_color(vehicle, c.r, c.g, c.b)

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied tire smoke", { name = preset.name })
    end

    return true
end

function module.apply_xenon(vehicle, color_id)
    if not does_entity_exist(vehicle) then return false end

    if color_id == nil or color_id < -1 then
        module.set_xenon_enabled(vehicle, false)
        return true
    end

    module.set_xenon_color(vehicle, color_id)

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied xenon", { id = color_id })
    end

    return true
end

function module.apply_wheel_type(vehicle, wheel_type_id)
    if not does_entity_exist(vehicle) then return false end

    if wheel_type_id == nil or wheel_type_id < 0 then
        return true  -- Keep stock wheels
    end

    module.set_wheel_type(vehicle, wheel_type_id)

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied wheel type", { id = wheel_type_id })
    end

    return true
end

-- -------------------------------------------------------------------------
-- Door/Window Control
-- -------------------------------------------------------------------------

function module.set_door_open(vehicle, door_index, open)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if open then
        if N.SET_VEHICLE_DOOR_OPEN then
            pcall(N.SET_VEHICLE_DOOR_OPEN, vehicle, door_index, false, false)
        end
    else
        if N.SET_VEHICLE_DOOR_SHUT then
            pcall(N.SET_VEHICLE_DOOR_SHUT, vehicle, door_index, false)
        end
    end
    return true
end

function module.open_all_doors(vehicle)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    for i = 0, 5 do
        module.set_door_open(vehicle, i, true)
    end
    return true
end

function module.close_all_doors(vehicle)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    for i = 0, 5 do
        module.set_door_open(vehicle, i, false)
    end
    return true
end

function module.set_window_down(vehicle, window_index, down)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if down then
        if N.ROLL_DOWN_WINDOW then
            pcall(N.ROLL_DOWN_WINDOW, vehicle, window_index)
        end
    else
        if N.ROLL_UP_WINDOW then
            pcall(N.ROLL_UP_WINDOW, vehicle, window_index)
        end
    end
    return true
end

function module.smash_window(vehicle, window_index)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SMASH_VEHICLE_WINDOW then
        pcall(N.SMASH_VEHICLE_WINDOW, vehicle, window_index)
    end
    return true
end

function module.fix_window(vehicle, window_index)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.FIX_VEHICLE_WINDOW then
        pcall(N.FIX_VEHICLE_WINDOW, vehicle, window_index)
    end
    return true
end

function module.roll_down_all_windows(vehicle)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    for i = 0, 3 do
        module.set_window_down(vehicle, i, true)
    end
    return true
end

function module.roll_up_all_windows(vehicle)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    for i = 0, 3 do
        module.set_window_down(vehicle, i, false)
    end
    return true
end

-- -------------------------------------------------------------------------
-- Convertible Roof Control
-- -------------------------------------------------------------------------

function module.is_convertible(vehicle)
    if not does_entity_exist(vehicle) then return false end
    if not N.IS_VEHICLE_A_CONVERTIBLE then return false end
    local ok, result = pcall(N.IS_VEHICLE_A_CONVERTIBLE, vehicle, false)
    return ok and result
end

function module.set_convertible_roof(vehicle, raised)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if raised then
        if N.RAISE_CONVERTIBLE_ROOF then
            pcall(N.RAISE_CONVERTIBLE_ROOF, vehicle, false)
        end
    else
        if N.LOWER_CONVERTIBLE_ROOF then
            pcall(N.LOWER_CONVERTIBLE_ROOF, vehicle, false)
        end
    end
    return true
end

function module.get_convertible_roof_state(vehicle)
    if not does_entity_exist(vehicle) then return 0 end
    if not N.GET_CONVERTIBLE_ROOF_STATE then return 0 end
    local ok, result = pcall(N.GET_CONVERTIBLE_ROOF_STATE, vehicle)
    return ok and result or 0
end

-- -------------------------------------------------------------------------
-- Engine/Lights Control
-- -------------------------------------------------------------------------

function module.set_engine_on(vehicle, on)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_ENGINE_ON then
        pcall(N.SET_VEHICLE_ENGINE_ON, vehicle, on, true, true)
    end
    return true
end

function module.set_vehicle_lights(vehicle, light_state)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_LIGHTS then
        pcall(N.SET_VEHICLE_LIGHTS, vehicle, light_state)
    end
    return true
end

function module.set_hazard_lights(vehicle, on)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_INDICATOR_LIGHTS then
        pcall(N.SET_VEHICLE_INDICATOR_LIGHTS, vehicle, 0, on)  -- left
        pcall(N.SET_VEHICLE_INDICATOR_LIGHTS, vehicle, 1, on)  -- right
    end
    return true
end

function module.set_left_indicator(vehicle, on)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_INDICATOR_LIGHTS then
        pcall(N.SET_VEHICLE_INDICATOR_LIGHTS, vehicle, 0, on)
    end
    return true
end

function module.set_right_indicator(vehicle, on)
    if not does_entity_exist(vehicle) then return false end
    ensure_control(vehicle)
    if N.SET_VEHICLE_INDICATOR_LIGHTS then
        pcall(N.SET_VEHICLE_INDICATOR_LIGHTS, vehicle, 1, on)
    end
    return true
end

-- -------------------------------------------------------------------------
-- Extras Control
-- -------------------------------------------------------------------------

function module.does_extra_exist(vehicle, extra_id)
    if not does_entity_exist(vehicle) then return false end
    if not N.DOES_EXTRA_EXIST then return false end
    local ok, result = pcall(N.DOES_EXTRA_EXIST, vehicle, extra_id)
    return ok and result
end

function module.is_extra_enabled(vehicle, extra_id)
    if not does_entity_exist(vehicle) then return false end
    if not N.IS_VEHICLE_EXTRA_TURNED_ON then return false end
    local ok, result = pcall(N.IS_VEHICLE_EXTRA_TURNED_ON, vehicle, extra_id)
    return ok and result
end

-- -------------------------------------------------------------------------
-- Stand/Cherax Vehicle Format Support
-- -------------------------------------------------------------------------

function module.apply_stand_mods(vehicle, stand_data)
    if not does_entity_exist(vehicle) then return false end
    if not stand_data or type(stand_data) ~= "table" then return false end

    ensure_control(vehicle)

    -- Enable mod kit
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end

    -- Wheel Type (0-12)
    if stand_data["Wheel Type"] then
        module.set_wheel_type(vehicle, stand_data["Wheel Type"])
    end

    -- Custom Tires flag
    local has_custom_tires = stand_data["Tyres Mode"] and stand_data["Tyres Mode"] == 1

    -- Front Wheels (mod slot 23)
    if stand_data["Front Wheels"] and stand_data["Front Wheels"] > 0 then
        module.set_front_wheels(vehicle, stand_data["Front Wheels"] - 1, has_custom_tires)
    end

    -- Back Wheels (mod slot 24, for motorcycles)
    if stand_data["Back Wheels"] and stand_data["Back Wheels"] > 0 then
        local is_bike = N.GET_VEHICLE_CLASS and N.GET_VEHICLE_CLASS(vehicle) == 8
        if is_bike then
            module.set_back_wheels(vehicle, stand_data["Back Wheels"] - 1, has_custom_tires)
        end
    end

    -- Colors (with custom color support)
    local use_custom_primary = (stand_data["Primary Colour"] == 0 and stand_data["Custom Primary Colour"] and stand_data["Custom Primary Colour"] ~= "")
    local use_custom_secondary = (stand_data["Secondary Colour"] == 0 and stand_data["Custom Secondary Colour"] and stand_data["Custom Secondary Colour"] ~= "")

    if use_custom_primary and N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR then
        -- Parse hex color (e.g., "FF0000" → RGB)
        local hex = stand_data["Custom Primary Colour"]
        local r = tonumber(hex:sub(1,2), 16) or 0
        local g = tonumber(hex:sub(3,4), 16) or 0
        local b = tonumber(hex:sub(5,6), 16) or 0
        pcall(N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR, vehicle, r, g, b)
    elseif stand_data["Primary Colour"] then
        local primary = stand_data["Primary Colour"]
        local secondary = stand_data["Secondary Colour"] or 0
        module.set_vehicle_color(vehicle, primary, secondary)
    end

    if use_custom_secondary and N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR then
        local hex = stand_data["Custom Secondary Colour"]
        local r = tonumber(hex:sub(1,2), 16) or 0
        local g = tonumber(hex:sub(3,4), 16) or 0
        local b = tonumber(hex:sub(5,6), 16) or 0
        pcall(N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR, vehicle, r, g, b)
    end

    -- Pearlescent and Wheel colors
    if stand_data["Pearl Colour"] or stand_data["Wheel Colour"] then
        local pearl = stand_data["Pearl Colour"] or 0
        local wheel = stand_data["Wheel Colour"] or 0
        if N.SET_VEHICLE_EXTRA_COLOURS then
            pcall(N.SET_VEHICLE_EXTRA_COLOURS, vehicle, pearl, wheel)
        end
    end

    -- Interior and Dashboard colors
    if stand_data["Interior Colour"] and N.SET_VEHICLE_INTERIOR_COLOUR then
        pcall(N.SET_VEHICLE_INTERIOR_COLOUR, vehicle, stand_data["Interior Colour"])
    end

    if stand_data["Dashboard Colour"] and N.SET_VEHICLE_DASHBOARD_COLOUR then
        pcall(N.SET_VEHICLE_DASHBOARD_COLOUR, vehicle, stand_data["Dashboard Colour"])
    end

    -- All mod slots mapping (Stand format uses singular names)
    local mod_slots = {
        ["Spoiler"] = 0,
        ["Front Bumper"] = 1,
        ["Rear Bumper"] = 2,
        ["Sideskirt"] = 3,
        ["Exhaust"] = 4,
        ["Chassis"] = 5,
        ["Grille"] = 6,
        ["Hood"] = 7,
        ["Fender"] = 8,
        ["Right Fender"] = 9,
        ["Roof"] = 10,
        ["Engine"] = 11,
        ["Brakes"] = 12,
        ["Transmission"] = 13,
        ["Horns"] = 14,
        ["Suspension"] = 15,
        ["Armor"] = 16,
        ["Nitrous"] = 17,
        ["Subwoofer"] = 19,
        ["Trim"] = 27,
        ["Ornaments"] = 28,
        ["Dashboard"] = 29,
        ["Dial"] = 30,
        ["Door Speaker"] = 31,
        ["Seats"] = 32,
        ["Steering Wheel"] = 33,
        ["Shifter Leavers"] = 34,
        ["Plaques"] = 35,
        ["Speakers"] = 36,
        ["Trunk"] = 37,
        ["Hydraulics"] = 38,
        ["Engine Block"] = 39,
        ["Air Filter"] = 40,
        ["Struts"] = 41,
        ["Arch Cover"] = 42,
        ["Aerials"] = 43,
        ["Trim2"] = 44,
        ["Tank"] = 45,
        ["Left Door"] = 46,
        ["Right Door"] = 47,
        ["Plate Holder"] = 49,
        ["Vanity Plates"] = 50,
        ["Lightbar"] = 53,
    }

    for stand_key, slot in pairs(mod_slots) do
        if stand_data[stand_key] and stand_data[stand_key] > 0 then
            module.set_mod(vehicle, slot, stand_data[stand_key] - 1, false)
        end
    end

    -- Livery (mod 48 + SET_VEHICLE_LIVERY)
    if stand_data["Livery"] and stand_data["Livery"] > 0 then
        local livery_index = stand_data["Livery"] - 1
        module.set_mod(vehicle, 48, livery_index, false)
        if N.SET_VEHICLE_LIVERY then
            pcall(N.SET_VEHICLE_LIVERY, vehicle, livery_index)
        end
    end

    -- Turbo (toggle mod 18)
    if stand_data["Turbo"] ~= nil then
        module.toggle_mod(vehicle, 18, stand_data["Turbo"])
    end

    -- Tire Smoke (toggle mod 20)
    if stand_data["Tiresmoke"] ~= nil and N.TOGGLE_VEHICLE_MOD then
        pcall(N.TOGGLE_VEHICLE_MOD, vehicle, 20, stand_data["Tiresmoke"])
    end

    -- Window Tint
    if stand_data["Window Tint"] then
        module.set_window_tint(vehicle, stand_data["Window Tint"])
    end

    -- License Plate
    if stand_data["Plate Text"] and N.SET_VEHICLE_NUMBER_PLATE_TEXT then
        pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT, vehicle, stand_data["Plate Text"])
    end

    if stand_data["Plate Style"] and N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX then
        pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX, vehicle, stand_data["Plate Style"])
    end

    -- Tire Smoke Color (custom hex)
    if stand_data["Tyre Smoke Colour"] and stand_data["Tyre Smoke Colour"] ~= "" and N.SET_VEHICLE_TYRE_SMOKE_COLOR then
        local hex = stand_data["Tyre Smoke Colour"]
        local r = tonumber(hex:sub(1,2), 16) or 255
        local g = tonumber(hex:sub(3,4), 16) or 255
        local b = tonumber(hex:sub(5,6), 16) or 255
        pcall(N.SET_VEHICLE_TYRE_SMOKE_COLOR, vehicle, r, g, b)
    end

    -- Neon Lights (individual sides + color)
    if stand_data["Neon Colour"] and stand_data["Neon Colour"] ~= "" then
        local hex = stand_data["Neon Colour"]
        local r = tonumber(hex:sub(1,2), 16) or 255
        local g = tonumber(hex:sub(3,4), 16) or 0
        local b = tonumber(hex:sub(5,6), 16) or 255
        module.set_neon_color(vehicle, r, g, b)
    end

    local neon_map = {
        ["Neon Left"] = 0,
        ["Neon Right"] = 1,
        ["Neon Front"] = 2,
        ["Neon Back"] = 3,
    }

    for stand_key, neon_index in pairs(neon_map) do
        if stand_data[stand_key] ~= nil and N.SET_VEHICLE_NEON_ENABLED then
            pcall(N.SET_VEHICLE_NEON_ENABLED, vehicle, neon_index, stand_data[stand_key])
        end
    end

    -- Xenon Headlights
    if stand_data["Headlights"] and stand_data["Headlights"] > 0 then
        if N.TOGGLE_VEHICLE_MOD then
            pcall(N.TOGGLE_VEHICLE_MOD, vehicle, 22, true)  -- Enable xenon first
        end
        module.set_xenon_color(vehicle, stand_data["Headlights"])
    end

    -- Extras (1-15)
    for i = 1, 15 do
        local extra_key = "Extra " .. i
        if stand_data[extra_key] ~= nil then
            -- Stand format: Yes/No (true/false), but SET_VEHICLE_EXTRA uses inverted logic
            module.set_extra(vehicle, i, stand_data[extra_key])
        end
    end

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied Stand mods", { model = stand_data["Model Name"] })
    end

    return true
end

function module.apply_lexis_mods(vehicle, lexis_data)
    if not does_entity_exist(vehicle) then return false end
    if not lexis_data or type(lexis_data) ~= "table" then return false end

    ensure_control(vehicle)

    -- Enable mod kit
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end

    -- Wheel Type
    if lexis_data["wheel type"] then
        module.set_wheel_type(vehicle, lexis_data["wheel type"])
    end

    -- Mods (array format: index 0-48+)
    if lexis_data.mods and type(lexis_data.mods) == "table" then
        for i, mod_index in ipairs(lexis_data.mods) do
            local slot = i - 1  -- Lua arrays start at 1, slots start at 0
            if mod_index and mod_index >= 0 then
                module.set_mod(vehicle, slot, mod_index, false)
            end
        end
    end

    -- Colors (array: [primary, secondary])
    if lexis_data.colors and type(lexis_data.colors) == "table" then
        local primary = lexis_data.colors[1] or 0
        local secondary = lexis_data.colors[2] or 0
        module.set_vehicle_color(vehicle, primary, secondary)
    end

    -- Custom Primary Color (array: [r, g, b])
    if lexis_data["custom primary"] and type(lexis_data["custom primary"]) == "table" then
        local r = lexis_data["custom primary"][1] or 0
        local g = lexis_data["custom primary"][2] or 0
        local b = lexis_data["custom primary"][3] or 0
        if N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR then
            pcall(N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR, vehicle, r, g, b)
        end
    end

    -- Custom Secondary Color (array: [r, g, b])
    if lexis_data["custom secondary"] and type(lexis_data["custom secondary"]) == "table" then
        local r = lexis_data["custom secondary"][1] or 0
        local g = lexis_data["custom secondary"][2] or 0
        local b = lexis_data["custom secondary"][3] or 0
        if N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR then
            pcall(N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR, vehicle, r, g, b)
        end
    end

    -- Extra Colors (array: [pearl, wheel])
    if lexis_data["extra colors"] and type(lexis_data["extra colors"]) == "table" then
        local pearl = lexis_data["extra colors"][1] or 0
        local wheel = lexis_data["extra colors"][2] or 0
        if N.SET_VEHICLE_EXTRA_COLOURS then
            pcall(N.SET_VEHICLE_EXTRA_COLOURS, vehicle, pearl, wheel)
        end
    end

    -- Window Tint
    if lexis_data["window tint"] then
        module.set_window_tint(vehicle, lexis_data["window tint"])
    end

    -- Tire Smoke Color (array: [r, g, b])
    if lexis_data["tire color"] and type(lexis_data["tire color"]) == "table" then
        local r = lexis_data["tire color"][1] or 255
        local g = lexis_data["tire color"][2] or 255
        local b = lexis_data["tire color"][3] or 255
        if N.TOGGLE_VEHICLE_MOD then
            pcall(N.TOGGLE_VEHICLE_MOD, vehicle, 20, true)
        end
        if N.SET_VEHICLE_TYRE_SMOKE_COLOR then
            pcall(N.SET_VEHICLE_TYRE_SMOKE_COLOR, vehicle, r, g, b)
        end
    end

    -- Neon Lights (array: [left, right, front, back])
    if lexis_data["neon enabled"] and type(lexis_data["neon enabled"]) == "table" then
        for i = 0, 3 do
            local enabled = lexis_data["neon enabled"][i + 1]
            if enabled ~= nil and N.SET_VEHICLE_NEON_ENABLED then
                pcall(N.SET_VEHICLE_NEON_ENABLED, vehicle, i, enabled)
            end
        end
    end

    -- Neon Color (array: [r, g, b])
    if lexis_data["neon color"] and type(lexis_data["neon color"]) == "table" then
        local r = lexis_data["neon color"][1] or 255
        local g = lexis_data["neon color"][2] or 0
        local b = lexis_data["neon color"][3] or 255
        module.set_neon_color(vehicle, r, g, b)
    end

    -- License Plate
    if lexis_data["plate text"] and N.SET_VEHICLE_NUMBER_PLATE_TEXT then
        pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT, vehicle, lexis_data["plate text"])
    end

    if lexis_data["plate index"] and N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX then
        pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX, vehicle, lexis_data["plate index"])
    end

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied Lexis mods")
    end

    return true
end

function module.apply_cherax_mods(vehicle, cherax_data)
    if not does_entity_exist(vehicle) then return false end
    if not cherax_data or type(cherax_data) ~= "table" then return false end

    ensure_control(vehicle)

    -- Enable mod kit
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end

    -- Wheel Type
    if cherax_data.wheelType then
        module.set_wheel_type(vehicle, cherax_data.wheelType)
    end

    -- Mods (dynamic slots: mod0, mod1, mod23, etc.)
    if cherax_data.mods and type(cherax_data.mods) == "table" then
        for mod_key, mod_data in pairs(cherax_data.mods) do
            if type(mod_key) == "string" and mod_key:match("^mod(%d+)$") then
                local slot = tonumber(mod_key:match("^mod(%d+)$"))
                if slot and type(mod_data) == "table" and mod_data.index and mod_data.index > 0 then
                    local index = mod_data.index - 1
                    local custom_tires = mod_data.variation or false
                    module.set_mod(vehicle, slot, index, custom_tires)
                end
            end
        end
    end

    -- Custom Primary Color
    if cherax_data.isPrimaryColorCustom and cherax_data.customPrimaryColor then
        if cherax_data.customPrimaryColor.r and cherax_data.customPrimaryColor.g and cherax_data.customPrimaryColor.b then
            if N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR then
                pcall(N.SET_VEHICLE_CUSTOM_PRIMARY_COLOUR, vehicle,
                    cherax_data.customPrimaryColor.r,
                    cherax_data.customPrimaryColor.g,
                    cherax_data.customPrimaryColor.b)
            end
        end
    elseif cherax_data.primaryColor then
        local primary = cherax_data.primaryColor
        local secondary = cherax_data.secondaryColor or 0
        module.set_vehicle_color(vehicle, primary, secondary)
    end

    -- Custom Secondary Color
    if cherax_data.isSecondaryColorCustom and cherax_data.customSecondaryColor then
        if cherax_data.customSecondaryColor.r and cherax_data.customSecondaryColor.g and cherax_data.customSecondaryColor.b then
            if N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR then
                pcall(N.SET_VEHICLE_CUSTOM_SECONDARY_COLOUR, vehicle,
                    cherax_data.customSecondaryColor.r,
                    cherax_data.customSecondaryColor.g,
                    cherax_data.customSecondaryColor.b)
            end
        end
    end

    -- Pearlescent and Wheel Color
    if cherax_data.pearlescentColor or cherax_data.wheelColor then
        local pearl = cherax_data.pearlescentColor or 0
        local wheel = cherax_data.wheelColor or 0
        if N.SET_VEHICLE_EXTRA_COLOURS then
            pcall(N.SET_VEHICLE_EXTRA_COLOURS, vehicle, pearl, wheel)
        end
    end

    -- Window Tint
    if cherax_data.windowTint then
        module.set_window_tint(vehicle, cherax_data.windowTint)
    end

    -- Tire Smoke Color
    if cherax_data.tyreSmokeColor then
        -- Enable tire smoke first
        if N.TOGGLE_VEHICLE_MOD then
            pcall(N.TOGGLE_VEHICLE_MOD, vehicle, 20, true)
        end
        if cherax_data.tyreSmokeColor.r and cherax_data.tyreSmokeColor.g and cherax_data.tyreSmokeColor.b then
            if N.SET_VEHICLE_TYRE_SMOKE_COLOR then
                pcall(N.SET_VEHICLE_TYRE_SMOKE_COLOR, vehicle,
                    cherax_data.tyreSmokeColor.r,
                    cherax_data.tyreSmokeColor.g,
                    cherax_data.tyreSmokeColor.b)
            end
        end
    end

    -- Neon Kits (individual control)
    if cherax_data.neonKits then
        if cherax_data.neonKits.left ~= nil and N.SET_VEHICLE_NEON_ENABLED then
            pcall(N.SET_VEHICLE_NEON_ENABLED, vehicle, 0, cherax_data.neonKits.left)
        end
        if cherax_data.neonKits.right ~= nil and N.SET_VEHICLE_NEON_ENABLED then
            pcall(N.SET_VEHICLE_NEON_ENABLED, vehicle, 1, cherax_data.neonKits.right)
        end
        if cherax_data.neonKits.front ~= nil and N.SET_VEHICLE_NEON_ENABLED then
            pcall(N.SET_VEHICLE_NEON_ENABLED, vehicle, 2, cherax_data.neonKits.front)
        end
        if cherax_data.neonKits.back ~= nil and N.SET_VEHICLE_NEON_ENABLED then
            pcall(N.SET_VEHICLE_NEON_ENABLED, vehicle, 3, cherax_data.neonKits.back)
        end
        if cherax_data.neonKits.color and cherax_data.neonKits.color.r and cherax_data.neonKits.color.g and cherax_data.neonKits.color.b then
            module.set_neon_color(vehicle,
                cherax_data.neonKits.color.r,
                cherax_data.neonKits.color.g,
                cherax_data.neonKits.color.b)
        end
    end

    -- License Plate
    if cherax_data.plateText and N.SET_VEHICLE_NUMBER_PLATE_TEXT then
        pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT, vehicle, cherax_data.plateText)
    end

    if cherax_data.plateTextIndex and N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX then
        pcall(N.SET_VEHICLE_NUMBER_PLATE_TEXT_INDEX, vehicle, cherax_data.plateTextIndex)
    end

    -- Interior and Dashboard Color
    if cherax_data.interiorColor and N.SET_VEHICLE_INTERIOR_COLOUR then
        pcall(N.SET_VEHICLE_INTERIOR_COLOUR, vehicle, cherax_data.interiorColor)
    end

    if cherax_data.dashBoardColor and N.SET_VEHICLE_DASHBOARD_COLOUR then
        pcall(N.SET_VEHICLE_DASHBOARD_COLOUR, vehicle, cherax_data.dashBoardColor)
    end

    -- Extras
    if cherax_data.extras and type(cherax_data.extras) == "table" then
        for i = 1, 15 do
            if cherax_data.extras[i] ~= nil then
                module.set_extra(vehicle, i, cherax_data.extras[i])
            end
        end
    end

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Applied Cherax mods", { name = cherax_data.name })
    end

    return true
end

--- Export vehicle data in Lexis format
--- @param vehicle number The vehicle entity handle
--- @return table|nil The vehicle data in Lexis format, or nil if failed
function module.export_vehicle_data(vehicle)
    if not does_entity_exist(vehicle) then return nil end

    local data = {}

    -- Get model hash
    if N.GET_ENTITY_MODEL then
        local ok, model = pcall(N.GET_ENTITY_MODEL, vehicle)
        if ok then
            data.model = model
        end
    end
    if not data.model then
        print("[VehicleCustomizer] GET_ENTITY_MODEL not available")
        return nil
    end

    -- Enable mod kit for reading
    if N.SET_VEHICLE_MOD_KIT then
        pcall(N.SET_VEHICLE_MOD_KIT, vehicle, 0)
    end

    -- [Lexis Format] Get vehicle colors
    if N.GET_VEHICLE_COLOURS then
        local primary_ptr = pointer_int()
        local secondary_ptr = pointer_int()
        local ok = pcall(N.GET_VEHICLE_COLOURS, vehicle, primary_ptr, secondary_ptr)
        if ok then
            data.colors = {primary_ptr.value or 0, secondary_ptr.value or 0}
        else
            data.colors = {0, 0}
        end
        primary_ptr:free()
        secondary_ptr:free()
    else
        data.colors = {0, 0}
    end

    -- [Lexis Format] Custom decal (always false, false)
    data["custom decal"] = {false, false}

    -- [Lexis Format] Get custom primary color
    if N.GET_VEHICLE_CUSTOM_PRIMARY_COLOUR then
        local r_ptr = pointer_int()
        local g_ptr = pointer_int()
        local b_ptr = pointer_int()
        local ok = pcall(N.GET_VEHICLE_CUSTOM_PRIMARY_COLOUR, vehicle, r_ptr, g_ptr, b_ptr)
        if ok then
            data["custom primary"] = {r_ptr.value or 0, g_ptr.value or 0, b_ptr.value or 0}
        else
            data["custom primary"] = {0, 0, 0}
        end
        r_ptr:free()
        g_ptr:free()
        b_ptr:free()
    else
        data["custom primary"] = {0, 0, 0}
    end

    -- [Lexis Format] Get custom secondary color
    if N.GET_VEHICLE_CUSTOM_SECONDARY_COLOUR then
        local r_ptr = pointer_int()
        local g_ptr = pointer_int()
        local b_ptr = pointer_int()
        local ok = pcall(N.GET_VEHICLE_CUSTOM_SECONDARY_COLOUR, vehicle, r_ptr, g_ptr, b_ptr)
        if ok then
            data["custom secondary"] = {r_ptr.value or 0, g_ptr.value or 0, b_ptr.value or 0}
        else
            data["custom secondary"] = {0, 0, 0}
        end
        r_ptr:free()
        g_ptr:free()
        b_ptr:free()
    else
        data["custom secondary"] = {0, 0, 0}
    end

    -- [Lexis Format] Get extra color 5 (interior color)
    if N.GET_VEHICLE_INTERIOR_COLOUR then
        local color_ptr = pointer_int()
        local ok = pcall(N.GET_VEHICLE_INTERIOR_COLOUR, vehicle, color_ptr)
        if ok then
            data["extra color 5"] = color_ptr.value or 0
        else
            data["extra color 5"] = 0
        end
        color_ptr:free()
    else
        data["extra color 5"] = 0
    end

    -- [Lexis Format] Get extra colors (pearlescent and wheel color)
    if N.GET_VEHICLE_EXTRA_COLOURS then
        local pearl_ptr = pointer_int()
        local wheel_ptr = pointer_int()
        local ok = pcall(N.GET_VEHICLE_EXTRA_COLOURS, vehicle, pearl_ptr, wheel_ptr)
        if ok then
            data["extra colors"] = {pearl_ptr.value or 0, wheel_ptr.value or 0}
        else
            data["extra colors"] = {0, 0}
        end
        pearl_ptr:free()
        wheel_ptr:free()
    else
        data["extra colors"] = {0, 0}
    end

    -- [Lexis Format] Garage bitset (always -1)
    data["garage bitset"] = -1

    -- [Lexis Format] Get all mods (slots 0-48, total 49 elements)
    data.mods = {}
    for slot = 0, 48 do
        if N.GET_VEHICLE_MOD then
            local ok, mod_index = pcall(N.GET_VEHICLE_MOD, vehicle, slot)
            if ok and mod_index then
                data.mods[slot + 1] = mod_index
            else
                data.mods[slot + 1] = -1
            end
        else
            data.mods[slot + 1] = -1
        end
    end

    -- [Lexis Format] Get neon color
    if N.GET_VEHICLE_NEON_COLOUR then
        local r_ptr = pointer_int()
        local g_ptr = pointer_int()
        local b_ptr = pointer_int()
        local ok = pcall(N.GET_VEHICLE_NEON_COLOUR, vehicle, r_ptr, g_ptr, b_ptr)
        if ok then
            data["neon color"] = {r_ptr.value or 255, g_ptr.value or 255, b_ptr.value or 255}
        else
            data["neon color"] = {255, 255, 255}
        end
        r_ptr:free()
        g_ptr:free()
        b_ptr:free()
    else
        data["neon color"] = {255, 255, 255}
    end

    -- [Lexis Format] Get neon enabled state (4 elements: left, right, front, back)
    data["neon enabled"] = {false, false, false, false}
    if N.GET_VEHICLE_NEON_ENABLED then
        for i = 0, 3 do
            local ok, enabled = pcall(N.GET_VEHICLE_NEON_ENABLED, vehicle, i)
            if ok then
                data["neon enabled"][i + 1] = enabled or false
            end
        end
    end

    -- [Lexis Format] Get plate index (style)
    if N.GET_VEHICLE_NUMBER_PLATE_TEXT_INDEX then
        local ok, plate_index = pcall(N.GET_VEHICLE_NUMBER_PLATE_TEXT_INDEX, vehicle)
        if ok then
            data["plate index"] = plate_index or 0
        else
            data["plate index"] = 0
        end
    else
        data["plate index"] = 0
    end

    -- [Lexis Format] Get plate text (8 characters)
    if N.GET_VEHICLE_NUMBER_PLATE_TEXT then
        local ok, plate_text = pcall(N.GET_VEHICLE_NUMBER_PLATE_TEXT, vehicle)
        if ok and plate_text then
            data["plate text"] = plate_text
        else
            data["plate text"] = "        "
        end
    else
        data["plate text"] = "        "
    end

    -- [Lexis Format] Get tire smoke color
    if N.GET_VEHICLE_TYRE_SMOKE_COLOR then
        local r_ptr = pointer_int()
        local g_ptr = pointer_int()
        local b_ptr = pointer_int()
        local ok = pcall(N.GET_VEHICLE_TYRE_SMOKE_COLOR, vehicle, r_ptr, g_ptr, b_ptr)
        if ok then
            data["tire color"] = {r_ptr.value or 255, g_ptr.value or 255, b_ptr.value or 255}
        else
            data["tire color"] = {255, 255, 255}
        end
        r_ptr:free()
        g_ptr:free()
        b_ptr:free()
    else
        data["tire color"] = {255, 255, 255}
    end

    -- [Lexis Format] Get wheel type
    if N.GET_VEHICLE_WHEEL_TYPE then
        local ok, wheel_type = pcall(N.GET_VEHICLE_WHEEL_TYPE, vehicle)
        if ok then
            data["wheel type"] = wheel_type or 0
        else
            data["wheel type"] = 0
        end
    else
        data["wheel type"] = 0
    end

    -- [Lexis Format] Get window tint
    if N.GET_VEHICLE_WINDOW_TINT then
        local ok, tint = pcall(N.GET_VEHICLE_WINDOW_TINT, vehicle)
        if ok then
            data["window tint"] = tint or -1
        else
            data["window tint"] = -1
        end
    else
        data["window tint"] = -1
    end

    -- [Lexis Format Optional] Get bulletproof tires status
    if N.GET_VEHICLE_TYRES_CAN_BURST then
        local ok, can_burst = pcall(N.GET_VEHICLE_TYRES_CAN_BURST, vehicle)
        if ok then
            data.bulletproof = not can_burst  -- inverted: can_burst=false means bulletproof=true
        end
    end

    if debugger and debugger.debug then
        debugger.debug("[VehicleCustomizer] Exported vehicle data in Lexis format")
    end

    return data
end

return module
